import type { Router } from "express";
import type { Ioracle, IcallbackFn } from "../../types";
import { decrypt } from "../../utils/privateKey";

const Home = (router: Router, oracle: Ioracle, callbackFn: IcallbackFn) => {
  router.post("/menu", (_req, res) => {
    const { username } = _req.body;
    let sql = ` select * from xxcc_new_hrsystem_power_v where power_user = '${decrypt(
      username.trim()
    )}' order by parent_code,menu_code `;
    callbackFn(res, oracle.connHrm, { sql });
  });

  router.post("/getdept", async (_req, res) => {
    // const { username } = _req.body;
    // let sql = ` select count(*) c_count from user_info_n where where dept in ('CCH20','CFH20','CHH20') and account = '${decrypt(
    //   username
    // )}' `;
    try {
      // const result = await oracle.connChaos({ sql });
      // if (Number(result.data[0].c_count) > 0) {
        let sql =` select code from department where status = 'N' and org_code in ('CC','CF','CH') order by code `;
        callbackFn(res, oracle.connHrm, { sql });
      // } else {
      //   sql = `select xxcc_new_hrsystem_getdept('${decrypt( username)}') from dual`;
      //   const resDept = await oracle.connHrm({ sql });
      //   console.log(resDept);
      // }
    } catch {
      res.send({ code: 0, message: "数据库异常" });
    }
  });

  router.post("/getcourse", (_req, res) => {
    let sql = " select code,description from course order by code";
    callbackFn(res, oracle.connHrm, { sql });
  });

  return router;
};

export default Home;
